package com.durian.base.rxhttp.parse

import okhttp3.Response
import java.io.IOException
import java.lang.reflect.Type

/**
 * 说明：SimpleParser
 * <p/>
 * 作者：Fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2020/04/25 18:44
 * <p/>
 * 版本：version 1.0
 */

open class DefaultParser<T> : AbstractParser<T> {

    constructor()

    constructor(type: Type) : super(type)

    constructor(index: Int) : super() {
        mType = getActualTypeParameter(javaClass, index)!!
    }

    @Throws(IOException::class)
    override fun onParse(response: Response): T {
        checkResponse(response)
        return convertBean(response.body?.string() ?: "")
    }

    open fun convertBean(result: String): T {
        if (mType == String::class.java){
            return result as T
        }
        return GsonConverter.convert(result,mType)
    }

}